package com.winter.common;

import java.util.Random;

/**
 * 随机生成字符串工具类
 */
public class RandomStringGenerator {

    /**
     * 获取一定长度的随机字符串
     * 
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static final Random randomer = new Random(System.currentTimeMillis());

    /**
     * 生成随机数字
     * 
     * @param len
     * @return
     */
    public static String getRandomCode(int len) {
        int count = 0;
        char[] codeArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        StringBuffer codeBuf = new StringBuffer(len);
        while (count < len) {
            codeBuf.append(codeArray[randomer.nextInt(codeArray.length)]);
            count++;
        }
        return codeBuf.toString();
    }

}
